#include "../../include/sql.h"

// 查询账号密码是否正确
int sql_select_userInfo_by_account_password(char account[20], char password[20], char *data, int *userId)
{
    printf("查询账号密码是否正确---account:%s password:%s\n", account, password);
    char query[256];
    snprintf(query, sizeof(query),
             "SELECT id, gender, age, avatar_id, account, niceName, signature "
             "FROM user WHERE account = '%s' AND password = '%s'",
             account, password);
    // 执行查询[1,3](@ref)
    if (mysql_query(conn, query))
    {
        fprintf(stderr, "查询失败: %s\n", mysql_error(conn));
        return -1;
    }

    // 获取结果集[3,9](@ref)
    MYSQL_RES *result = mysql_store_result(conn);
    if (!result)
    {
        fprintf(stderr, "未获取到结果: %s\n", mysql_error(conn));
        return -1;
    }

    // 处理结果[4,7](@ref)
    MYSQL_ROW row = mysql_fetch_row(result);
    if (!row)
    {
        printf("账号或密码错误\n");
        mysql_free_result(result);
        return 0;
    }

    // 打印用户信息
    printf("登录成功！用户信息：\n");
    printf("id:%s, gender:%s, age:%s, avatar_id:%s, account:%s, niceName:%s, signature:%s\t",
           row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
    *userId = atoi(row[0]);
    snprintf((char *)data, 1024, "%d^%d^%s^%s^%s^%s^%s^%s^%s",
             1000, 1, row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
    printf("%s\n", data);
    return 1;
}
